package ran.ding;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 * Java 8 方法引用
 */
public class FunctionalInterface {
    static class Car {
        public static Car create(final Supplier<Car> supplier) {
            return supplier.get();
        }

        public static void collide(final Car car) {
            System.out.println("Collide:" + car.toString());
        }
        public void follow(final Car car){
            System.out.println("Following the "+ car.toString());
        }

        public void repair(){
            System.out.println("Repaired "+ this.toString());
        }
    }

    public static void main(String[] args) {
        //构造器引用
        final Car car = Car.create(Car::new);
        final List<Car> cars= Arrays.asList(car);
        //静态方法引用
        cars.forEach(Car:: collide);
        //特定类的任意对象的方法引用
        cars.forEach(Car::repair);
        //特定对象的方法引用
        final Car police = Car.create(Car::new);
        System.out.println("Police: "+ police.toString());
        cars.forEach(police::follow);
    }
}
